/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.texture;

import com.android1.amarena2d.commons.IntRectangle;
import com.android1.amarena2d.engine.EngineObject;
import com.badlogic.gdx.Files;
import com.badlogic.gdx.graphics.Texture;

public final class Textures extends EngineObject {

    private static final TextureBuilder TEXTURE_BUILDER = new TextureBuilder();

    private Textures() {
    }

    public static TextureBuilder $(String path) {
        return TEXTURE_BUILDER.init(path);
    }


    public static final class TextureBuilder implements Type {

        String path;
        Files.FileType fileType;
        TextureOptions textureOptions = TextureOptions.NEAREST;
        final IntRectangle rect = new IntRectangle();

        TextureBuilder() {
        }


        TextureBuilder init(String path) {
            this.path = path;
            this.textureOptions = TextureOptions.NEAREST;
            this.fileType = engine.getConfig().getDefaultFileType();
            this.rect.setEmpty();
            return this;
        }


        public TextureBuilder(String path) {
            this.path = path;
        }

        @Override
        public Options fileType(Files.FileType fileType) {
            this.fileType = fileType;
            return this;
        }

        @Override
        public Frame options(TextureOptions textureOptions) {
            this.textureOptions = new TextureOptions(textureOptions);
            return this;
        }

        @Override
        public Frame options(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap) {
            this.textureOptions = new TextureOptions(minFilter, magFilter, uWrap, vWrap);
            return this;
        }

        @Override
        public Get frame(int x, int y, int width, int height) {
            this.rect.set(x, y, width, height);
            return this;
        }

        @Override
        public ManagedTexture get() {
            ManagedTexture t = engine.getTextureManager().getTexture(path, fileType, textureOptions);
            if (!this.rect.isEmpty())
                t.setTextureFrame(rect.x, rect.y, rect.width, rect.height);
            return t;
        }

        @Override
        public TextureDef getDef() {
            TextureDef def = new TextureDef(path, fileType, textureOptions.minFilter, textureOptions.magFilter, textureOptions.uWrap, textureOptions.vWrap);
            return def;
        }


    }

    public static interface Type extends Options {
        public Options fileType(Files.FileType fileType);
    }


    public static interface Options extends Frame {

        public Frame options(TextureOptions textureOptions);

        public Frame options(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, Texture.TextureWrap uWrap, Texture.TextureWrap vWrap);
    }

    public static interface Frame extends Get {
        public Get frame(int left, int right, int top, int bottom);
    }


    public static interface Get {
        public ManagedTexture get();
        public TextureDef getDef();
    }


}
